package com.example.thirdhomework

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat


const val FIRST_MSG = "FIRST_MSG"
const val SECOND_MSG = "SECOND_MSG"
class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val button1 = findViewById<Button>(R.id.button1)
        val editText = findViewById<EditText>(R.id.editText)
        val button2 = findViewById<Button>(R.id.button2)
        val button3 = findViewById<Button>(R.id.botton3)
        val button4 = findViewById<Button>(R.id.button4)

        button1.setOnClickListener {
            val intent = Intent(this,SecondActivity::class.java)
            intent.putExtra(FIRST_MSG,editText.text.toString())
            startActivity(intent)
        }
        button2.setOnClickListener {
            val intent = Intent(this,ThirdActivity::class.java)
            startActivityForResult(intent,0)
        }
        button3.setOnClickListener {
            startActivity(Intent(this,ForthActivity::class.java))
        }
        button4.setOnClickListener {
            startActivity(Intent(this,FifthActivity::class.java))
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        val textView = findViewById<TextView>(R.id.textView)

        if(requestCode == 0) {
            if (resultCode == 1) {
                textView.text = data?.getStringExtra(SECOND_MSG)
            }
        }
    }
}

private fun Intent.putExtra(firstMsg: String, editText: EditText?) {

}
